import types from '@/store/constants/types'
import filter from '@/lib/filter'
import store from '@/store'
import {
        GetAreaList,
        GetMissionPerformance,
        GetWayBillDataTrend,
        GetAlarmInformation,
        GetWaybillWeight,
        GetStationList,
        GetRegistrationNum
    } from '@/api/screenApi';

export default {
    async [types.HOME_GET_DATA] (context, amount) { //获取首页数据
        try {
            if(amount && !amount.timer){
                store.commit(types.SWITCH_LOADING, true)
            }
            

            let params = {
                areaCode:amount?amount.areaCode:"",
                stationId:amount?amount.stationId:""
            }

            const that = this
            
            if(amount && amount.type==1){
                //获取区域列表
                const AreaList = await GetAreaList()
                
                let plantDistribute = {data:[],concreteNum:0,stationNum:0,wharfNum:0};
                
                // plantDistribute.wharfNum = ComapnyList.wharfNum
                // plantDistribute.concreteNum = ComapnyList.concreteNum
                // plantDistribute.stationNum = ComapnyList.stationNum

                if(AreaList.length > 0){
                    AreaList.forEach(el => {
                        plantDistribute.data.push([el.longitude, el.latitude, el.number, el.name,el.areaCode])
                    })
                    
                    //plantDistribute.data = ComapnyList.weightStationVoList

                }
                context.commit( types.HOME_SET_PLANT_DISTRIBUTE, plantDistribute)
            }else if(amount && amount.type==2){
                //获取企业列表
                const CompanyInfo = await GetStationList(params)
                let plantDistribute = {data:[],concreteNum:0,stationNum:0,wharfNum:0};
                
                plantDistribute.wharfNum = CompanyInfo.wharfNum
                plantDistribute.concreteNum = CompanyInfo.concreteNum
                plantDistribute.stationNum = CompanyInfo.stationNum

                if(CompanyInfo.weightStationVoList.length > 0){
                    
                    plantDistribute.data = CompanyInfo.weightStationVoList

                }
                context.commit( types.HOME_SET_PLANT_DISTRIBUTE, plantDistribute)
            }
            
            
            //获取各区任务执行情况
            const MissPerInfo = await GetMissionPerformance(params);//各区上传情况
            
            
            let originRegion = {data:[]}

            if(MissPerInfo) {
                let Origin = []
                Origin.push({
                    "gislate": 25.81751,
                    "name": "总数量",
                    "count": MissPerInfo.totalNum,
                    "gislong": 124.92085
                },{
                    "gislate": 39.9011531576,
                    "name": "处理中",
                    "count": MissPerInfo.inProcessNum,
                    "gislong": 116.389046224
                },
                {
                    "gislate": 31.2339572483,
                    "name": "已处理",
                    "count": MissPerInfo.processedNum,
                    "gislong": 121.4934393989
                },
                {
                    "gislate": 22.5385199653,
                    "name": "已核实",
                    "count": MissPerInfo.verifiedNum,
                    "gislong": 114.078918457
                },
                {
                    "gislate": 29.0414352756,
                    "name": "已退回",
                    "count": MissPerInfo.returnedNum,
                    "gislong": 113.2733528646
                },
                // {
                //     "gislate": 29.0414352756,
                //     "name": "不处理",
                //     "count": MissPerInfo.untreatedNum,
                //     "gislong": 113.2733528646
                // }
                )
                originRegion = filter.obj(Origin, 'name', 'count')
            }
            originRegion.data.sort(function (a, b) {
                return b.value - a.value
            })
            if(originRegion.data.length > 8){
                originRegion.data = originRegion.data.splice(0, 9)
                originRegion.data.push({name: '...', value: ''})
            }
            context.commit( types.HOME_SET_ORIGIN_REGION, originRegion )


            //车辆超载统计
            const CarOver = await GetRegistrationNum(params)
            
            let inventoryContrast = { data: [] }

            if(CarOver) {
                let Stock = []
                Stock.push({
                    "cropName": "驾驶员",
                    "number": CarOver.driverNum
                },
                {
                    "cropName": "车辆",
                    "number": CarOver.vehicleNum
                })
                inventoryContrast = filter.obj(Stock, 'cropName', 'number', 2, false)
            }
            inventoryContrast.unit = '辆'
            
            context.commit( types.HOME_SET_INVENTORY_CONTRAST, inventoryContrast )

            //每日运单走势
            const BillUpload = await GetWayBillDataTrend(params)
            
            let fertQuantity = {
                titles: [],
                values: [],
                names: [],
                unit: '次'
            }
            
            if(BillUpload.length > 0) {
                fertQuantity.titles = BillUpload.map(el=>el.wayBillDate)
                fertQuantity.values = BillUpload.map(el=>el.wayBillNum)
                fertQuantity.names  = BillUpload.map(el=>el.wayBillDate)
            }
            context.commit( types.HOME_SET_FERT_QUANTITY, fertQuantity )

            //各区预警处置
            const AlarmInfo = await GetAlarmInformation(params)
            
            let pickRank = { 
                titles: [],
                values: [],
                names: [],
                unit: '家'
            }
            if(AlarmInfo.length > 0) {
                pickRank.titles = AlarmInfo.map(el=>el.areaName)
                let disposedNum = AlarmInfo.map(el=>el.disposedNum)
                let noDisposedNum = AlarmInfo.map(el=>el.noDisposedNum)
                pickRank.values.push(disposedNum,noDisposedNum)
                pickRank.names = AlarmInfo.map(el=>el.areaName)
            }
            context.commit( types.HOME_SET_PICK_RANK, pickRank )

            //获取运单称重重量
            const BillWeight = await GetWaybillWeight(params);

            let outboundInventory = {
                titles: [], 
                values: [], 
                names:[],
                unit: '吨'
            }
            if(BillWeight.length > 0) {
                outboundInventory.titles = BillWeight.map(el=>el.month)
                outboundInventory.names = BillWeight.map(el=>el.month)
                outboundInventory.values = BillWeight.map(el=>el.totalWeight)
            }
            
            context.commit( types.HOME_SET_OUTBOUND_INVENTORY, outboundInventory )
            
            
            that.commit( 'map/' + types.MAP_SET_POST_DATA, amount ) //保存当前页面的状态
            store.commit(types.SWITCH_LOADING, false)
        } catch (error) {
            store.commit(types.SWITCH_LOADING, false)
            // eslint-disable-next-line no-console
            console.log("error==",error)
        }
        

        
    }
}